package ca.nfb.media.nfbPlayer.ui
{
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.net.*;
	import flash.utils.ByteArray;
	import flash.system.LoaderContext;
	
	public class NFBImage extends Sprite
	{
		public static const IMAGE_LOADING	:String = "imageLoading";
		public static const IMAGE_LOADED	:String = "imageLoaded";
		
		private var _imageURL			:String = "";
		
		public var bitmapImage			:Bitmap;
		private var bitmapHolder_mc		:MovieClip = new MovieClip();
		private var canvas_mc			:MovieClip = new MovieClip();
		private var bitmapLoader		:Loader = new Loader();
		private var bitmapImageData		:ByteArray;
		private var requiredWidth		:Number = 100;
		private var requiredHeight		:Number = 60;
		private var totalHeight			:Number;
		private var totalWidth			:Number;
		public var maintainAspectRatio	:Boolean = false;
		private var context				:LoaderContext;
		
		public function NFBImage(w:Number, h:Number, url:String=null)
		{
			super();
			
			if (w)
				requiredWidth = w;
			
			if (h)
				requiredHeight = h;
			
			if (url)
				_imageURL = url;
			
			context = new LoaderContext();
			context.checkPolicyFile = true;

			bitmapLoader = new Loader();
			bitmapLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
			bitmapLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError, false, 0, true);
			
			bitmapHolder_mc.addChild(bitmapLoader);	
			
			addChild(canvas_mc);
		}
		
		public function clear():void
		{
			if (this.contains(canvas_mc))
			{
				if (canvas_mc.contains(bitmapHolder_mc))
				{
					canvas_mc.removeChild(bitmapHolder_mc);		
				}
			}
		}
		
		public function startLoading(url:String=null):void
		{
			this.dispatchEvent(new Event(NFBImage.IMAGE_LOADING));
			if (url)
				_imageURL = url;
				
			bitmapLoader.load(new URLRequest(_imageURL), context);
		}
		
		private function onComplete(evt:Event):void
		{
			var bmp:Bitmap = Bitmap(bitmapLoader.content);
			bmp.smoothing = true;
			
			totalWidth = bitmapHolder_mc.width;
			totalHeight = bitmapHolder_mc.height;
			sizeCheck();
			canvas_mc.addChild(bitmapHolder_mc);
			
			if (bitmapHolder_mc.width < requiredWidth)
				canvas_mc.x = (requiredWidth - bitmapHolder_mc.width)/2;
			
			if (bitmapHolder_mc.height < requiredHeight)
				canvas_mc.y = (requiredHeight - bitmapHolder_mc.height)/2;
				
			this.dispatchEvent(new Event(NFBImage.IMAGE_LOADED));	
		}
		
		private function onError(evt:IOErrorEvent):void
		{
			
		}
		
		private function sizeCheck():void
		{
			if (bitmapHolder_mc.width != requiredWidth)
			{
				bitmapHolder_mc.width = requiredWidth;
				bitmapHolder_mc.scaleY = bitmapHolder_mc.scaleX;
			}
			
			if (bitmapHolder_mc.height != requiredHeight)
			{
				bitmapHolder_mc.height = requiredHeight;
				bitmapHolder_mc.scaleX = bitmapHolder_mc.scaleY;
			}
		}
		
		public function resize(w:Number, h:Number):void
		{
			requiredWidth = w;
			requiredHeight = h;
			sizeCheck();
			if (bitmapHolder_mc.width < requiredWidth)
				canvas_mc.x = (requiredWidth - bitmapHolder_mc.width)/2;
			
			if (bitmapHolder_mc.height < requiredHeight)
				canvas_mc.y = (requiredHeight - bitmapHolder_mc.height)/2;
		}
		
	}
}